import json
import os
from datetime import datetime
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
from django.db.models import Q
from django.shortcuts import render
from django.http import JsonResponse, HttpResponse
from django.template import loader
from django.utils import timezone
from django.core import serializers
from django.views.decorators.http import require_POST, require_GET
from pure_pagination.mixins import PaginationMixin

# Create your views here.
from django.views.generic import ListView, DetailView
from django.views.generic.base import View

from devops.settings import BASE_DIR
from .models import WorkFlow
from django.contrib.auth import get_user_model
User = get_user_model()


class WorkFlowAddView(LoginRequiredMixin, PermissionRequiredMixin,View):
    """
      添加通用工单
    """
    permission_required = 'workflow.add_workflow'

    def get(self, request):
        tags = ['普通类工单',
            '功能类工单',
            'BUG类工单',
            '系统服务类工单']
        context = {'tags':tags}
        return render(request, 'workflow/generalwork_add.html', context=context)

    def post(self, request):
        content = request.POST.get('content')
        work_title = request.POST.get('work_title')
        work_type = request.POST.get('work_type')

        workflow = WorkFlow()
        workflow.apply_user = request.user
        workflow.status = 0
        workflow.content = content
        workflow.title = work_title
        workflow.type = work_type
        workflow.save()
        # emails = [user.email for user in User.objects.all() if user.has_perm('task.review_task')]
        # email_send.delay(emails, send_type='audit_task', task=task.name)
        return JsonResponse({'code': 0, 'msg': '任务创建成功'})

class WorkFlowListView(LoginRequiredMixin, PermissionRequiredMixin, PaginationMixin, ListView):
    template_name = 'workflow/workflow_list.html'
    model = WorkFlow
    paginate_by = 10
    permission_required = 'workflow.view_task'
    keyword = None

    def get_keyword(self):
        self.keyword = self.request.GET.get('keyword')
        return self.keyword

    def get_queryset(self):
        queryset = super().get_queryset()
        queryset = queryset.filter(status__in=[0, 2])
        keyword = self.get_keyword()
        # queryset = queryset.filter(Q(title__icontains=keyword) |
        #                            Q(type__icontains=keyword)|
        #                            Q(content__icontains=keyword) |
        #                            Q(apply_user__username__icontains=keyword) |
        #                            Q(apply_user__name__icontains=keyword) |
        #                            Q(result__icontains=keyword)).distinct()
        return queryset

    def get_context_data(self, *args, **kwargs):
        context = super().get_context_data(*args, **kwargs)
        context['keyword'] = self.keyword
        return context

class WorkFlowDetailView(LoginRequiredMixin, PermissionRequiredMixin,DetailView):
    pass

class WorkFlowHistoryListView(LoginRequiredMixin, PermissionRequiredMixin, PaginationMixin,ListView):
    pass
